package com.thoughtworks.studio.tools.cardkit.gui;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.prefs.Preferences;

import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.LookAndFeel;
import javax.swing.text.DefaultEditorKit;

import org.jdesktop.layout.GroupLayout;
import org.jhotdraw.app.Application;
import org.jhotdraw.util.prefs.PreferencesUtil;

public class FindDialog extends javax.swing.JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6184991931402063035L;
	private Application app;
	private Preferences prefs;
	private String cardnumber;


	/** Creates new form FindDialog */
	private FindDialog(Frame parent, boolean modal) {
		super(parent, modal);

		initComponents();

		setAlwaysOnTop(true);
		prefs = Preferences.userNodeForPackage(getClass());

		PreferencesUtil.installFramePrefsHandler(prefs, "find", this);

		getRootPane().setDefaultButton(buttonPanel);

		InputMap im = new InputMap();

		LookAndFeel.loadKeyBindings(im, new String[] { "shift ENTER",
				DefaultEditorKit.insertBreakAction, "alt ENTER",
				DefaultEditorKit.insertBreakAction, "ENTER",
				JTextField.notifyAction, });

		im.setParent(findField.getInputMap(JComponent.WHEN_FOCUSED));
		findField.setInputMap(JComponent.WHEN_FOCUSED, im);
		im = new InputMap();
		LookAndFeel.loadKeyBindings(im, new String[] { "shift ENTER",
				DefaultEditorKit.insertBreakAction, "alt ENTER",
				DefaultEditorKit.insertBreakAction, "ENTER",
				JTextField.notifyAction, });
		pack();
	}

	/** Creates new form FindDialog */
	public FindDialog(Application app) {
		this(null, true);
		this.app = app;
	}
	

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code
	// ">//GEN-BEGIN:initComponents
	private void initComponents() {

		findScrollPane = new javax.swing.JScrollPane();

		findLabel = new javax.swing.JLabel();
		findField = new javax.swing.JTextArea();
		buttonPanel = new javax.swing.JButton("Find");
		buttonPanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addButtonPanelActionListener();
			}

		});

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Find");
		findLabel.setText("Story Number: ");

		findField.setLineWrap(true);
		findField.setRows(1);
		findScrollPane.setViewportView(findField);

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setAutocreateGaps(true);
		layout.setAutocreateContainerGaps(true);

		layout.setHorizontalGroup(layout.createSequentialGroup().add(findLabel)
				.add(findField).add(buttonPanel));

		GroupLayout.ParallelGroup vpg1 = layout
				.createParallelGroup(GroupLayout.BASELINE);
		vpg1.add(findLabel);
		vpg1.add(findField);
		vpg1.add(buttonPanel);
		layout.setVerticalGroup(layout.createSequentialGroup().add(vpg1));
		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void addButtonPanelActionListener() {
		cardnumber = findField.getText();
		System.out.println("FindDialog.cardnumber="+cardnumber);
		if (cardnumber!=null) {
			this.dispose();
		} else {
			findField.requestFocus();
		}
	}

	public javax.swing.JTextArea findField;
	public javax.swing.JLabel findLabel;
	public javax.swing.JScrollPane findScrollPane;
	public javax.swing.JButton buttonPanel;
	// End of variables declaration//GEN-END:variables


	public String getCardnumber() {
		return cardnumber;
	}
}
